package com.ycp.erp.brand.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.config.RouterPath;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.widget.MyTitleBar;
import com.ycp.erp.R;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * Class Des:品牌备注
 * Created by bjh on 2021/8/25.
 */
@Route(path = RouterPath.BRAND_REMARK)
public class BrandRemarkActivity extends BaseActivity {
    @BindView(R.id.etRemark)
    EditText etRemark;
    @BindView(R.id.tvCount)
    TextView tvCount;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;

    @Override
    protected void initPresenter() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_brand_remark;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("填写备注信息").hideLine();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        super.initView();
        DefaultExtra defaultExtra = (DefaultExtra) getIntent().getSerializableExtra(DefaultExtra.getExtraName());
        if (StringUtils.isNotBlank(defaultExtra.getStr())){
            etRemark.setText(defaultExtra.getStr());
        }
        tvSubmit.setText("确认提交");
        etRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvCount.setText(String.format("%s/120", etRemark.getText().toString().length()));
            }
        });
        RxView.clicks(tvSubmit).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            if (StringUtils.isNotBlank(etRemark.getText().toString())){
                Intent intent = new Intent();
                intent.putExtra(DefaultExtra.getExtraName(), new DefaultExtra(etRemark.getText().toString()));
                setResult(RESULT_OK, intent);
                finish();
            }else {
                Toaster.showShortToast("请填写备注信息");
            }

        });
    }
}
